<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="addJiao"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table :data="settinglist">
              <el-table-column label="序号" width="100" type="index" :index="indexMethod" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="addsetting(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="bian(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delList(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
        <el-pagination
          background
          :current-page="page"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        <!--     @current-change="handleCurrentChange" 页码改变时候，触发该事件 -->
        </el-pagination>
      </el-card>
    </div>
    <SettDat ref="setdt" :show-dialog.sync="showDialog" :set-addlist="setAddlist" />
    <SettOpen ref="refenc" :show-assign-dialog.sync="showAssignDialog" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import SettOpen from '@/views/setting/settingopents/index.vue'
import { getCompanyByIdApi } from '@/api/company'
import SettDat from '@/views/setting/components/setting.vue'
import { SettingListAPI, delListAPI } from '@/api/setting'
export default {
  name: 'Dashboard',
  components: {
    SettDat,
    SettOpen
  },
  data() {
    return {
      page: 1,
      pagesize: 10,
      settinglist: [], // 角色列表,
      total: 0,
      showDialog: false,
      showAssignDialog: false,
      setAddlist: [],
      getlist: [],
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      }
    }
  },
  computed: {
    ...mapState('user', ['userInfo']),
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.settingList()// 获取角色列表
    this.getCompanyInfo() // 获取公司信息
  },
  methods: {
    /**
     * 点击显示分配权限弹层
     */
    addsetting(id) {
      this.showAssignDialog = true
      this.$refs.refenc.retdid = id
    },
    async getCompanyInfo() {
      const { data } = await getCompanyByIdApi(this.userInfo.companyId)
      this.companyForm = data
    },
    /**
     * 获取列表数据
     */
    async settingList() {
      const { data: res } = await SettingListAPI({
        page: this.page,
        pagesize: this.pagesize
      })
      console.log(res)
      this.settinglist = res.rows
      // console.log(this.settinglist)
      this.total = res.total
      // console.log(this.total)
    },
    /**
     *  console.log(val)  参数表示修改后的值
     */
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.settingList()
    },
    // 页码切换后，触发的方法
    handleCurrentChange(val) {
      // console.log(val) // 表示切换后的页码值
      this.page = val // 比如点击3，修改数据项为3 // 重新发送请求，获取第3页的数据
      this.settingList()
    },
    /**
     * 删除角色
   */
    async  delList(id) {
      console.log(id)
      this.$confirm('确认删除吗', '提示').then(async() => {
        await delListAPI(id)
        if (this.settinglist.length === 1 && this.page > 1) {
          this.page--
        }
        this.settingList()
        this.$message.success('删除成功')
      })
    },
    /**
     * 处理序号
     */
    indexMethod(index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    /**
     * 点击显示弹出层，获取所有数据验证不能相同
     */
    addJiao() {
      this.showDialog = true
      this.setAddlist = this.settinglist
      // console.log(this.setAddlist)
    },
    bian(row) {
      this.showDialog = true
      this.getlist = this.settinglist
      this.$nextTick(() => {
        this.$refs.setdt.getxiugai(row)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.block{
  float: right;
}
</style>

